package com.example.booksystem.view.page;

import com.example.booksystem.controller.UserController;
import com.example.booksystem.model.User;
import com.example.booksystem.view.Index;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.sql.SQLException;
import java.util.List;

public class UserManage {
    private static final UserController controller = new UserController();
    private static Stage stage;
    private static User myUser;
    public static void show(User user){
        myUser = user;
        //修改用户权限为次级管理员&普通用户
        //删除用户
        stage = new Stage();
        VBox vBox = new VBox();
        createBox(vBox);
        Scene scene = new Scene(vBox,440,600);
        stage.setScene(scene);
        stage.setTitle("用户管理");
        stage.show();
    }

    private static void createBox(VBox vBox) {
        List<User> users;
        try {
            users = controller.selectUser();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        String[] user = {"用户名","用户级别","操作"};
        for (String u : user) {
            HBox box = new HBox();
            box.setAlignment(Pos.CENTER);
            box.setPrefWidth(140);
            Label label = new Label(u);
            box.getChildren().add(label);
            hBox.getChildren().add(box);
        }
        vBox.getChildren().add(hBox);
        for (int i = 0; i < users.size(); i++) {
            hBox = new HBox();
            Label[] labels = new Label[2];
            labels[0] = new Label(users.get(i).getUsername());
            Integer id = users.get(i).getId();
            Integer type = users.get(i).getUserType();
            Button button = new Button("无法操作");
            switch (type){
                case 0:
                    labels[1] = new Label("超级管理员");
                    break;
                case 1:
                    labels[1] = new Label("管理员");
                    if (myUser.getUserType() < 1){
                        button = new Button("降为普通用户");
                        button.setOnAction(e->{
                            try {
                                if (new UserController().toNomal(id)){
                                    Index.success("修改用户权限成功");
                                }
                            } catch (SQLException ex) {
                                throw new RuntimeException(ex);
                            }
                        });
                    }

                    break;
                case 2:
                    labels[1] = new Label("普通用户");
                    button = new Button("升为管理员");
                    button.setOnAction(e->{
                        try {
                            if (new UserController().toAdmin(id)){
                                Index.success("修改用户权限成功");
                            }
                        } catch (SQLException ex) {
                            throw new RuntimeException(ex);
                        }
                    });
                    break;
            }
            for (int j = 0; j < 2; j++) {
                HBox box = new HBox();
                box.setAlignment(Pos.CENTER);
                box.setPrefWidth(140);
                box.getChildren().add(labels[j]);
                hBox.getChildren().add(box);
            }
            HBox box = new HBox();
            box.setAlignment(Pos.CENTER);
            box.setPrefWidth(160);
            box.getChildren().add(button);
            if (type > myUser.getUserType()){
                Button delete = new Button("删除用户");
                delete.setOnAction(e->{
                    try {
                        if (new UserController().deteleUser(id)){
                            Index.success("删除用户成功");
                        }
                    } catch (SQLException ex) {
                        throw new RuntimeException(ex);
                    }
                });
                box.getChildren().add(delete);
            }
            hBox.getChildren().add(box);
            vBox.getChildren().add(hBox);
        }
    }
}
